package cn.wolfcode.shop.controller;

import cn.wolfcode.shop.api.domain.Brand;
import cn.wolfcode.shop.api.domain.Catalog;
import cn.wolfcode.shop.api.domain.Product;
import cn.wolfcode.shop.api.page.PageResult;
import cn.wolfcode.shop.api.query.BrandQueryObject;
import cn.wolfcode.shop.api.service.IBrandService;
import cn.wolfcode.shop.api.service.ICatalogService;
import cn.wolfcode.shop.api.service.IProductService;
import cn.wolfcode.shop.api.vo.JsonResultVo;
import cn.wolfcode.shop.util.UploadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


@Controller
public class BrandController {

    @Reference
    private IBrandService brandService;

    @Reference
    private IProductService productService;

    @Reference
    private ICatalogService catalogService;

    @Value("${file.path}")
    private String filePath;

    @RequestMapping("brandList")
    public String query(Model model, @ModelAttribute("qo") BrandQueryObject qo) {
        qo.setDisplayState(1);  //只展示状态值为1 的数据
        PageResult result = brandService.query(qo);
        model.addAttribute("pageResult", result);

        List<Product> products = productService.listProducts();
        List<Catalog> catalogs = catalogService.listCatalogs();
        System.out.println("---------products-----------" + products.size());
        System.out.println("---------catalogs-----------" + catalogs.size());

        model.addAttribute("products", products);
        model.addAttribute("catalogs", catalogs);
        return "brand/brand";
    }

    @RequestMapping("recoveryBrandList")
    public String recoveryBrand(Model model, @ModelAttribute("qo") BrandQueryObject qo) {
        qo.setDisplayState(0);  //只展示状态值为0 的数据
        PageResult result = brandService.query(qo);
        System.out.println("---------------" + result.getCurrentPage());
        System.out.println("---------------" + result.getPageSize());
        model.addAttribute("pageResult", result);
        return "brand/brandRecovery";
    }


    //新增保存
    @RequestMapping("editBrand")
    @ResponseBody
    public JsonResultVo brandedit(Brand brand,  // 接收页面集合类型的数据
                                  @RequestParam("brandCatalogs") List<String> brandCatalogs,
                                  @RequestParam("underProducts") List<String> underProducts) {
        System.out.println("-----------brand----------" + brand);

        System.out.println("----------" + brandCatalogs.toString());
        System.out.println("----------" + underProducts.toString());

        JsonResultVo jsonResult = new JsonResultVo();

        if (brand.getId() != null) {
            try {
                brandService.editBrandSave(brand);
                jsonResult.setSuccess(true);
                jsonResult.setErrorMsg("保存成功");
            } catch (Exception e) {
                e.printStackTrace();
                jsonResult.setSuccess(false);
                jsonResult.setErrorMsg("保存失败, 失败原因:" + e.getMessage());
            }
            return jsonResult;
        } else {
            try {
                System.out.println("----------" + brand.getBrandCatalogs().toString());
                System.out.println("----------" + brand.getUnderProducts().toString());

                brandService.addBrandSave(brand);

                jsonResult.setSuccess(true);
                jsonResult.setErrorMsg("保存成功");
            } catch (Exception e) {
                e.printStackTrace();
                jsonResult.setSuccess(false);
                jsonResult.setErrorMsg("保存失败, 失败原因:" + e.getMessage());
            }
            return jsonResult;
        }
    }


    //删除记录--由于此表, 在其他表中有外键约束, 只能通过显示状态控制页面数据的显示
    @RequestMapping("deleteBrand")
    @ResponseBody
    public JsonResultVo deleteBrand(Long id) {
        JsonResultVo jsonResult = new JsonResultVo();
        if (id != null) {
            try {
                brandService.deleteBrand(id);
                jsonResult.setSuccess(true);
                jsonResult.setErrorMsg("删除成功");
                return jsonResult;
            } catch (Exception e) {
                e.printStackTrace();
                jsonResult.setSuccess(false);
                jsonResult.setErrorMsg("删除失败, 失败原因:" + e.getMessage());
                return jsonResult;
            }
        }
        return jsonResult;
    }


    //恢复记录
    @RequestMapping("recoveryBrand")
    @ResponseBody
    public JsonResultVo recovery(Long id) {
        JsonResultVo jsonResult = new JsonResultVo();
        if (id != null) {
            try {
                brandService.recoveryBrand(id);
                jsonResult.setSuccess(true);
                jsonResult.setErrorMsg("数据回收成功");
                return jsonResult;
            } catch (Exception e) {
                e.printStackTrace();
                jsonResult.setSuccess(false);
                jsonResult.setErrorMsg("数据回收成功, 失败原因:" + e.getMessage());
                return jsonResult;
            }
        }
        return jsonResult;
    }


    //文件上传: 借助工具类完后才能完成上傳, 数据库保存的是 文件的地址
    @RequestMapping("uploadImg")
    @ResponseBody
    public String uploadLogo(MultipartFile image, Long id) {

        //String realPath = request.getServletContext().getRealPath("/") 从request中取，得到webapp路径。
        //String realPath = request.getSession().getServletContext().getRealPath("/") ; //拼接文件夹

        if (id != null) {
            System.out.println("---------------filePath---------------" + filePath);
            String fileName = UploadUtil.upload(image, filePath);
            String logoURL = fileName;
            brandService.saveLogoUrl(logoURL, id);
            return logoURL; //返回文件全名称, 便于在页面回显图片, 不要带磁盘路径名称, 否则无法回显
        } else {

            String fileName = UploadUtil.upload(image, filePath);
            String logoURL = fileName;
            brandService.saveNewLogoUrl(logoURL);
            return logoURL;

        }


    }


}
